package cn.woblog.android.common.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class NetworkUtils {
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager != null) {
			NetworkInfo[] networkInfo = manager.getAllNetworkInfo();
			if (networkInfo != null) {
				for (int i = 0; i < networkInfo.length; i++) {
					if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static void toNetworkSetingActivity(Context context) {
		Intent intentToNetwork = new Intent("/");
		ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
		intentToNetwork.setComponent(componentName);
		intentToNetwork.setAction("android.intent.action.VIEW");
		context.startActivity(intentToNetwork);
	}

	public static boolean isInWifi(Context context) {
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo info = connectMgr.getActiveNetworkInfo();

		if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}
		return false;
	}

	public static void showIn234NetworkTypeToast(Context context) {
		if (!isInWifi(context)) {
			// ToastAlone.showToast((Activity) context, "正在使用2G/3G/4G", 1);
			Toast.makeText(context, "正在使用2G/3G/4G", Toast.LENGTH_SHORT).show();
		}
	}

	public static void showIn234NetworkTypeToast1(Context context) {
		Toast.makeText(context, "正在使用2G/3G/4G",  Toast.LENGTH_SHORT).show();
	}
}
